<?php
class Admin_user_m extends CI_Model{
    function __construct(){
        parent::__construct();
        $this->load->database();
    }
    public function get_all_users($per_page,$off_set){
        $this->db->select('
        users_profiles.first_name,
        users_profiles.last_name,
        users_profiles.display_name,
        users_profiles.mobile,
        users.email,
        users.group_id,
        users.created_on,
        users.active,
        users.last_login,
        users.id
        ');
        $this->db->from('users,users_profiles');
        $this->db->where('users.id = users_profiles.user_id');
        $this->db->where('users.group_id',3);
        $this->db->limit($per_page,$off_set);
        return $this->db->get()->result_array();
    }
    public function count_all_users(){
        $this->db->select('
        users_profiles.first_name,
        users_profiles.last_name,
        users_profiles.display_name,
        users.email,
        users.group_id,
        users.created_on,
        users.last_login,
        users.id
        ');
        $this->db->from('users,users_profiles');
        $this->db->where('users.id = users_profiles.user_id');
        $this->db->where('users.group_id',3);
        return count($this->db->get()->result_array());
    }

    public function register( $password, $email, $additional_data = false)
    {
        $group_name = 'user';
        $group_id = $this->db->select('id')
        ->where('name', $group_name)
        ->get('users_groups')
        ->row()->id;

        // IP Address
        $ip_address = $this->input->ip_address();
        $password = md5($password);

        // Users table.
        $data = array(
        'password'   => $password,
        'email'      => $email,
        'group_id'   => $group_id,
        'ip_address' => $ip_address,
        'created_on' => now(),
        'last_login' => now(),
        'active'     => 1
        );
        if($this->db->insert('users',$data)>0){
            // Profile table.
            $id = $this->db->insert_id();
            $data = array('user_id' => $id);
            $columns = array('display_name','first_name','last_name','company','dob','gender','phone','mobile','address','gravatar');
            if (!empty($columns))
            {
                foreach ($columns as $input)
                {
                    if (is_array($additional_data) && isset($additional_data[$input]))
                    {
                        $data[$input] = $additional_data[$input];
                    }
                    elseif ($this->input->post($input))
                    {
                        $data[$input] = $this->input->post($input);
                    }
                }
            }                    
        }
        return $this->db->insert('users_profiles', $data)>0;
    }
    public function get_user_by_id($id){
        $this->db->select(
        'users_profiles.*,
        users.email,
        users.group_id,
        users_groups.name,
        users.active,
        users.id,
        '

        );
        $this->db->from('users,users_groups,users_profiles');
        $this->db->where('users.id = users_profiles.user_id');
        $this->db->where('users.group_id = users_groups.id');
        $this->db->where('users.group_id',3);
        $this->db->where('users.id',$id);
        return $this->db->get()->row_array();
    }
    public function edit_profile($user_id,$data_user,$data_profile) {
        $this->db->update('users', $data_user,array('id'=>$user_id));
        return $this->db->update('users_profiles', $data_profile,array('user_id'=>$user_id));
    }
    public function delete_user($id){
        $this->db->delete('users_profiles',array('user_id'=>$id));
        $this->db->delete('address',array('user_id'=>$id));
        return $this->db->delete('users',array('id'=>$id)); 
    }
    public function get_exist_email($email){
        if(empty($email)){
            return FALSE;
        }
        return $this->db->where('email',$email)
        ->count_all_results('users')>0;
    }

    public function delete_users($list) {
        $this->db->where('id in ' . $list);
        $query = $this->db->delete('users');
        $this->db->where('user_id in ' . $list);
        $query1 = $this->db->delete('users_profiles');
        return $query;

    }
    public function deactivate_user($id){
        return $this->db->update('users',array('active'=>0),array('id'=>$id));
    }
    public function active_user($id){
        return $this->db->update('users',array('active'=>1),array('id'=>$id));
    }
    function get_address_by_user_id($user_id){
        return $this->db->get_where('address',array('user_id'=>$user_id))->result_array();
    }
    function total_paid_history_by_user_id($user_id){
        $this->db->select('(SELECT SUM(orders.total_paid)
                        FROM orders
                        WHERE orders.id_user='.$user_id.'
                        and orders.status_id = 1)
                        as amount_paid', FALSE);
        return $query = $this->db->get('orders')->row_array();
    }
    function is_vip_user($user_id){
        $query = $this->total_paid_history_by_user_id($user_id);
        return $query['amount_paid'] >= 20000000;
    }
    function count_vip_user(){
        $count_vip = 0;
        $this->db->select('
        users_profiles.first_name,
        users_profiles.last_name,
        users_profiles.display_name,
        users.email,
        users.group_id,
        users.created_on,
        users.last_login,
        users.id
        ');
        $this->db->from('users,users_profiles');
        $this->db->where('users.id = users_profiles.user_id');
        $this->db->where('users.group_id',3);
        $query = $this->db->get()->result_array();
        for($i=0;  $i<count($query);$i++){
            if($this->is_vip_user($query[$i]['id'])){
                $count_vip++;
            }
        }
        return $count_vip;
    }
    function count_normal_user(){
        $count_normal = 0;
        $this->db->select('
        users_profiles.first_name,
        users_profiles.last_name,
        users_profiles.display_name,
        users_profiles.mobile,
        users.email,
        users.group_id,
        users.created_on,
        users.last_login,
        users.id
        ');
        $this->db->from('users,users_profiles');
        $this->db->where('users.id = users_profiles.user_id');
        $this->db->where('users.group_id',3);
        $query = $this->db->get()->result_array();
        for($i=0;  $i<count($query);$i++){
            if($this->is_normal_user($query[$i]['id'])){
                $count_normal++;
            }
        }
        return $count_normal;
    }
    function is_normal_user($user_id){
        $query = $this->total_paid_history_by_user_id($user_id);
        return $query['amount_paid'] < 20000000;
    }
    function all_order_by_user($user_id,$lang,$per_page, $off_set){
        $this->db->select('orders.id_order,orders.total_paid, users_profiles.first_name,users_profiles.last_name,orders.payment, status_lang.status_id,status_lang.name, orders.date_add');
        $this->db->from('orders,users_profiles,status_lang');
        $this->db->where('orders.id_user = users_profiles.user_id');
        $this->db->where('orders.status_id = status_lang.status_id');
        $this->db->where('status_lang.lang_id',$lang);
        $this->db->where('orders.id_user',$user_id);
        $this->db->limit($per_page, $off_set);
        return $this->db->get()->result_array();
    }
    function count_all_order_by_user($user_id,$lang){
        $this->db->select('orders.id_order,orders.total_paid, users_profiles.first_name,users_profiles.last_name,orders.payment, status_lang.status_id,status_lang.name, orders.date_add');
        $this->db->from('orders,users_profiles,status_lang');
        $this->db->where('orders.id_user = users_profiles.user_id');
        $this->db->where('orders.status_id = status_lang.status_id');
        $this->db->where('status_lang.lang_id',$lang);
        $this->db->where('orders.id_user',$user_id);
        return count($this->db->get()->result_array());
    }
}
